package com.codeduck.common.response;

import com.codeduck.common.enums.ResEnum;
import lombok.Data;

import java.io.Serializable;

/**
 * @author: <a href="mailto:codeduck@163.com">Codeduck</a>
 * @createDate: 2022-08-19
 */
@Data
public class ServiceResponse<T> implements Serializable {

    private static final long serialVersionUID = -2088579976785672441L;

    private String code;

    private T data;

    private String message;

    public static <T> ServiceResponse<T> asOk(T data) {
        ServiceResponse<T> serviceResponse = new ServiceResponse<>();
        serviceResponse.setCode(ResEnum.SUCCESS.getCode());
        serviceResponse.setData(data);
        serviceResponse.setMessage(ResEnum.SUCCESS.getMessage());
        return serviceResponse;
    }

    public static <T> ServiceResponse<T> asFailed(String message) {
        ServiceResponse<T> serviceResponse = new ServiceResponse<>();
        serviceResponse.setCode(ResEnum.SUCCESS.getCode());
        serviceResponse.setData(null);
        serviceResponse.setMessage(message);
        return serviceResponse;
    }
}
